﻿package net.wangit.framework.expression;

import net.wangit.LabelUtil;
import net.wangit.context.ActionContext;
import net.wangit.util.Checker;
import net.wangit.util.StringUtil;
/**
 * 国际化标签表达式
 * @author watson
 *
 */
public class LabelExpression {

	private static final String PREFIX = "$L{";
	private static final String SUFFIX = "}";

	public LabelExpression() {
	}

	public static final String resolve(ActionContext ac, String expression) {
		if (Checker.isEmpty(expression))
			return expression;
		StringBuffer result = new StringBuffer();
		String attribute;
		while (!Checker.isEmpty(attribute = StringUtil.find(expression,
				LabelExpression.PREFIX, LabelExpression.SUFFIX))) {
			int index = expression.indexOf((new StringBuilder(
					LabelExpression.PREFIX)).append(attribute).append(
					LabelExpression.SUFFIX).toString());
			result.append(expression.substring(0, index)).append(
					getValue(ac, attribute));
			expression = expression.substring(index + attribute.length()
					+ LabelExpression.PREFIX.length()
					+ LabelExpression.SUFFIX.length());
		}
		result.append(expression);
		return result.toString();
	}

	private static final String getValue(ActionContext ac, String attribute) {
		int index = attribute.indexOf(":");
		if (index < 1)
			return LabelUtil.get(ac, attribute);
		else
			return LabelUtil.get(ac, attribute.substring(0, index), attribute
					.substring(index + 1));
	}
}
